/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Employee;
import com.click.app.model.LabelValue;
import com.click.app.service.EmployeeManager;
import com.click.app.service.SalaryManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @author orgale
 */
public class EmployeeAction extends BaseAction {
 
    private EmployeeManager employeeManager;
    private SalaryManager salaryManager;
    private List employees;
    private Employee employee;
    private Long id;
    private List<LabelValue> roleListLeft;
    private List<LabelValue> roleListRight;
    private Set<String> selectRoles;

    public void setId(Long id) {
        this.id = id;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public List<LabelValue> getRoleListLeft() {
        return roleListLeft;
    }

    public List<LabelValue> getRoleListRight() {
        return roleListRight;
    }

    public Set<String> getSelectRoles() {
        return selectRoles;
    }

    public void setSelectRoles(Set<String> selectRoles) {
        this.selectRoles = selectRoles;
    }

    public String delete() {
        employeeManager.remove(employee.getId());
        saveMessage(getText("employee.deleted"));

        return SUCCESS;
    }

    public String edit() {
        roleListLeft = new ArrayList<LabelValue>();
        LabelValue lV1 = new LabelValue(getText("employee.cutter"), "ROLE_EMPLOYEE_CUTTER");
        LabelValue lV2 = new LabelValue(getText("employee.dressmaker"), "ROLE_EMPLOYEE_DRESSMAKER");
        LabelValue lV3 = new LabelValue(getText("employee.fixed"), "ROLE_EMPLOYEE_FIXED");
        roleListLeft.add(lV1);
        roleListLeft.add(lV2);
        roleListLeft.add(lV3);
        roleListRight = new ArrayList<LabelValue>();

        if (id != null) {
            employee = employeeManager.get(id);
            if (employee.isEmployeeCutter()) {
                roleListRight.add(lV1);
                roleListLeft.remove(lV1);
            }
            if (employee.isEmployeeDressmaker()) {
                roleListRight.add(lV2);
                roleListLeft.remove(lV2);
            }
            if (employee.isEmployeeFixed()) {
                roleListRight.add(lV3);
                roleListLeft.remove(lV3);
            }
        } else {
            employee = new Employee();
            employee.setSalarieFixed(0f);
        }

        getSession().setAttribute("employee", employee);

        return SUCCESS;
    }

    public String save() throws Exception {
        System.out.println("saving employee...");

        boolean existErrors = false;
        
        if(employee.getFirstName() == null || employee.getFirstName().matches("")){
            errorMessage("Nombre es un campo requerido.");
            existErrors = true;
        }
        if(employee.getLastName() == null || employee.getLastName().matches("")){
            errorMessage("Apellido Paterno es un campo requerido.");
            existErrors = true;
        }
        if(employee.getEmail() == null || employee.getEmail().matches("")){
            errorMessage("E-Mail es un campo requerido.");
            existErrors = true;
        }
        if(employee.getAddress().getCity() == null || employee.getAddress().getCity().matches("")){
            errorMessage("Ciudad es un campo requerido.");
            existErrors = true;
        }
        if(employee.getAddress().getProvince() == null || employee.getAddress().getProvince().matches("")){
            errorMessage("Provincia es un campo requerido.");
            existErrors = true;
        }
        if(employee.getAddress().getCountry() == null || employee.getAddress().getCountry().matches("")){
            errorMessage("Pais es un campo requerido.");
            existErrors = true;
        }
        if (selectRoles == null){
            errorMessage(getText("employee.errorRole"));
            roleListLeft = new ArrayList<LabelValue>();
            LabelValue lV1 = new LabelValue(getText("employee.cutter"), "ROLE_EMPLOYEE_CUTTER");
            LabelValue lV2 = new LabelValue(getText("employee.dressmaker"), "ROLE_EMPLOYEE_DRESSMAKER");
            LabelValue lV3 = new LabelValue(getText("employee.fixed"), "ROLE_EMPLOYEE_FIXED");
            roleListLeft.add(lV1);
            roleListLeft.add(lV2);
            roleListLeft.add(lV3);
            roleListRight = new ArrayList<LabelValue>();
            existErrors = true;
        } else {
            roleListLeft = new ArrayList<LabelValue>();
            LabelValue lV1 = new LabelValue(getText("employee.cutter"), "ROLE_EMPLOYEE_CUTTER");
            LabelValue lV2 = new LabelValue(getText("employee.dressmaker"), "ROLE_EMPLOYEE_DRESSMAKER");
            LabelValue lV3 = new LabelValue(getText("employee.fixed"), "ROLE_EMPLOYEE_FIXED");
            roleListLeft.add(lV1);
            roleListLeft.add(lV2);
            roleListLeft.add(lV3);
            roleListRight = new ArrayList<LabelValue>();
            for(String s : selectRoles){
                if(s.equals("ROLE_EMPLOYEE_CUTTER")){
                    roleListRight.add(lV1);
                    roleListLeft.remove(lV1);
                }
                if(s.equals("ROLE_EMPLOYEE_DRESSMAKER")){
                    roleListRight.add(lV2);
                    roleListLeft.remove(lV2);
                }
                if(s.equals("ROLE_EMPLOYEE_FIXED")){
                    roleListRight.add(lV3);
                    roleListLeft.remove(lV3);
                    if(employee.getSalarieFixed() == null || employee.getSalarieFixed() < 600){
                        errorMessage("El Salario Fijo es un campo requerido y debe ser mayor o igual al salario minimo de 600 Bs.");
                        existErrors = true;
                    }
                }
            }
        }
        if(employee.getTask() == null || employee.getTask().matches("")){
            errorMessage("Tarea/Cargo es un campo requerido.");
            existErrors = true;
        }
        if(employee.getEntryDate() == null){
            errorMessage("Fecha de entrada es un campo requerido.");
            existErrors = true;
        }

        if(existErrors)
            return INPUT;

        boolean isNew = (employee.getId() == null);

        if (isNew) {
            employee.setUsername(employee.getFirstName() + "." + employee.getLastName());
            employee.setPassword("");
            employee.setAccountExpired(false);
            employee.setAccountLocked(false);
            employee.setCredentialsExpired(false);
            for (String s : selectRoles) {
                employee.addRole(roleManager.getRole(s));
            }
        } else {
            employee.clearEmployeeRoles(userManager.getUser(employee.getId().toString()));
            for (String s : selectRoles) {
                employee.addRole(roleManager.getRole(s));
            }
        }

        employee = employeeManager.save(employee);

        String key = (isNew) ? "employee.added" : "employee.updated";
        saveMessage(getText(key));

        return SUCCESS;
    }

    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    public void setSalaryManager(SalaryManager salaryManager) {
        this.salaryManager = salaryManager;
    }

    public List getEmployees() {
        return employees;
    }

    public String list() {
        System.out.println("listando usuario: " + getRequest().getRemoteUser());
        employees = employeeManager.getAllDistinct();

        return SUCCESS;
    }

    public String help() {
        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }
}
